<?php
function graph_colors($im) {
  $colors["background"] = ImageColorAllocate($im, 255, 255, 255);
  $colors["text"] = ImageColorAllocate($im, 0, 0, 0);
  $colors[0] = ImageColorAllocate($im, 255, 80, 80);
  $colors[1] = ImageColorAllocate($im, 254, 255, 80);
  $colors[2] = ImageColorAllocate($im, 81, 80, 255);
  $colors[3] = ImageColorAllocate($im, 255, 80, 254);
  $colors[4] = ImageColorAllocate($im, 80, 255, 81);
  $colors[5] = ImageColorAllocate($im, 80, 255, 255);
  $colors[6] = ImageColorAllocate($im, 34, 139, 34);
  $colors[7] = ImageColorAllocate($im, 255, 140, 0);
  $colors[8] = ImageColorAllocate($im, 255, 0, 0);
  $colors[9] = ImageColorAllocate($im, 0, 191, 255);

  $colors_dark[0] = ImageColorAllocate($im, 155, 0, 0);
  $colors_dark[1] = ImageColorAllocate($im, 154, 155, 0);
  $colors_dark[2] = ImageColorAllocate($im, 0, 0, 155);
  $colors_dark[3] = ImageColorAllocate($im, 155, 0, 154);
  $colors_dark[4] = ImageColorAllocate($im, 0, 155, 0);
  $colors_dark[5] = ImageColorAllocate($im, 0, 155, 155);
  $colors_dark[6] = ImageColorAllocate($im, 0, 100, 0);
  $colors_dark[7] = ImageColorAllocate($im, 205, 102, 0);
  $colors_dark[8] = ImageColorAllocate($im, 205, 0, 0);
  $colors_dark[9] = ImageColorAllocate($im, 0, 154, 205);
  
  return array("colors" => $colors, "colors_dark" => $colors_dark);
 }
function graph_pie_3d($piece = array(), $piece_text = array(), $height, $width, $height3d, $no_sort) {
  header("Content-type: image/png");
  $im = ImageCreateTrueColor($width, $height);

  $graph_colors = graph_colors($im);

  $colors = $graph_colors["colors"];
  $colors_dark = $graph_colors["colors_dark"];

  ImageFill($im, 0, 0, $colors["background"]);

  $sum = array_sum($piece);
  $height_graph = $height - ImageFontHeight(2) * count($piece) - $height3d - 15;
  $width_graph = $width - 20;
  $x_graph = $width / 2;
  $y_graph = $height_graph / 2 + $height3d + 10;

  if (!$no_sort) arsort($piece);

  for ($i = 0; $i <= $height3d; $i++) {
   $l = 0;
   $j = 0;

   while (list($key, $val) = each($piece)) {
    $degree = $val / $sum * 360;

    ImageFilledArc($im, $x_graph, $y_graph - $i, $width_graph, $height_graph, $l, $l + $degree, $colors_dark[$j], IMG_ARC_PIE);

    $l+= $degree;
    $j++;
   }

   reset($piece);
  }

  reset($piece);
  $l = 0;
  $j = 0;

  while (list($key, $val) = each($piece)) {
   $degree = $val / $sum * 360;

   ImageFilledArc($im, $x_graph, $y_graph - $height3d, $width_graph, $height_graph, $l, $l + $degree, $colors[$j], IMG_ARC_PIE);

   $y_legend = $height_graph + $height3d + ImageFontHeight(2) * ($j + 1);

   ImageFilledRectangle($im, 10, $y_legend + 2, 18, $y_legend + ImageFontHeight(2) - 2, $colors[$j]);
   ImageRectangle($im, 10, $y_legend + 2, 18, $y_legend + ImageFontHeight(2) - 2, $text);
   ImageString($im, 2, 24, $y_legend, $piece_text[$key]." (".$val.")", $colors["text"]);

   $l+= $degree;
   $j++;
  }

  ImagePNG ($im);
 }  
?>
